/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.infodb.metrics.web;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;

/**
 * @project-name:infodb
 * @package-name:com.hyts.infodb.metrics.web
 * @author:LiBo/Alex
 * @create-date:2020-01-16 19:19
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:
 */
@Slf4j
@RestController
public class ShutdownController {


    @Autowired
    RestTemplate restTemplate;

    private static final String SHUTDOWN_PATH = "/shutdown";

    private static final String DEFAULT_ACTUTOR_PATH = "/actuator"+SHUTDOWN_PATH;

    /**
     * 执行操作相关的关闭服务操作
     * @param request
     * @return
     */
    @GetMapping(SHUTDOWN_PATH)
    public String shutdown(HttpServletRequest request){
        log.info("执行关闭操作请求路径:{}",request.getRequestURL().toString());
        try {
            restTemplate.postForEntity("http://localhost:8881/shutdown".replace(SHUTDOWN_PATH,DEFAULT_ACTUTOR_PATH),new JSONObject(),String.class);
        } catch (RestClientException e) {
            log.error("执行关闭操作请求路径失败！",e);
            return "failure";
        }
        return "success";
    }

}
